package com.pedro.streamer;

import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;



public class AudioPlayer {


    Context context;
//    SimpleExoPlayer player;
    Handler handler;
    volatile boolean isInit = false;
//    com.example.vcam.AudioPlayer@7fe1690
    private AudioPlayer() {
    }

    public static AudioPlayer getSingleton() {
        Log.e("yyyyyyyyy", "getSingleton: "+Inner.instance);
        return Inner.instance;
    }

    private static class Inner {
        private static final AudioPlayer instance = new AudioPlayer();
    }

    public synchronized void init(Context context) {
        if (isInit) {

            return;
        }
        isInit = true;
        Log.e("yyyyyyyyy", "audio init start ");
        HandlerThread handlerThread = new HandlerThread("ExoPlayerThread");
        handlerThread.start();
        handler = new Handler(handlerThread.getLooper());

        this.context = context;
//        AdaptiveTrackSelection.Factory trackSelectionFactory = new AdaptiveTrackSelection.Factory();
//        DefaultTrackSelector trackSelector = new DefaultTrackSelector(/* context= */ context, trackSelectionFactory);

        handler.post(() -> {
//            player = new SimpleExoPlayer.Builder(/* context= */ context)
//                    .setTrackSelector(trackSelector)
//                    .build();
        });
        Log.e("yyyyyyyyy", "audio init success ");
    }

    public void setDataSource(String audioUrl) {

        handler.post(() -> {
            Uri url = Uri.parse(audioUrl);
//            DefaultExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
//
//            MediaSource audioSource = new ProgressiveMediaSource.Factory(
//                    new DefaultDataSourceFactory(context, "ExoPlayer"),extractorsFactory)
//                    .createMediaSource(url);
//            player.prepare(audioSource);
        });
    }

    public void start() {
        handler.post(() -> {
//            player.play();
        });
    }

    public void prepare() {

    }

    public void stop() {
        handler.post(() -> {
//            player.stop();
        });

    }

    public void reset() {

    }

    public void release() {
        handler.post(() -> {
//            player.release();
        });
    }


    public void setVolume(int left, int right) {
        handler.post(() -> {
//            player.setVolume(left);
        });
    }

    public void setLooping(boolean loop) {

    }


}
